//
//  YHYLeftBarViewController.m
//  TripTogether
//
//  Created by 杨泓晔 on 15/12/24.
//  Copyright © 2015年 杨泓晔. All rights reserved.
//

#import "YHYLeftBarViewController.h"
#import "YHYHunterCityModel.h"
@interface YHYLeftBarViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) UITableView *myTableView;
@property (nonatomic,strong)NSMutableArray *cityArray;
@property (nonatomic,strong)NSString *cityName;

@end

@implementation YHYLeftBarViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self getData];
    
    self.view.backgroundColor = [UIColor colorWithRed:0.286 green:0.827 blue:0.851 alpha:1.000];
    
#pragma mark headLabel城市
    UILabel *headLabel = [UILabel new];
    headLabel.textAlignment = NSTextAlignmentCenter;
    headLabel.backgroundColor = [UIColor colorWithRed:0.286 green:0.827 blue:0.851 alpha:1.000];
    [self.view addSubview:headLabel];
    headLabel.text = @"城市选择";
    headLabel.textColor = [UIColor colorWithRed:0.9879 green:0.9839 blue:0.9998 alpha:1.0];
    
    
    [headLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(@0);
        make.top.mas_equalTo(30);
        make.height.mas_equalTo(@30);
    }];
    
#pragma mark _myTableView
    
    
    _myTableView = [UITableView new];
    [self.view addSubview:_myTableView];
    
    
    
    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(headLabel.mas_bottom).mas_equalTo(20);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-100);
        
    }];
    _myTableView.dataSource = self;
    _myTableView.delegate = self;
    _myTableView.backgroundColor = [UIColor colorWithRed:0.286 green:0.827 blue:0.851 alpha:1.000];
   
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    //_myTableView的线
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; _myTableView.separatorColor = [UIColor colorWithWhite:0.965 alpha:1.000];
    [_myTableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 15)];
    
    
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
   // button.backgroundColor = [UIColor redColor];
    [button setImage:[UIImage imageNamed:@"iconfont-x.png"] forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchDown];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_myTableView.mas_bottom).offset(20);
        make.right.left.mas_equalTo(@0);
        
    }];
    
    
    
}
- (void)getData{
#pragma mark 请求数据
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/hunter/products/tabs/v2/"];
    _cityArray = [NSMutableArray array];
    [WJQNetwork getAFNet:url block:^(id result) {
        
        _cityArray  =result[@"hunter_city_list"];
        
        [_myTableView reloadData];
    } errorBlock:^(NSError *error) {
        
        
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _cityArray.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        static NSString *cellHead = @"cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellHead];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.textLabel.text = @"全部城市";
        cell.textLabel.textColor = [UIColor colorWithRed:0.953 green:0.991 blue:0.991 alpha:0.430];
        cell.backgroundColor = [UIColor colorWithRed:0.286 green:0.827 blue:0.851 alpha:1.000];
        
        [cell.textLabel setFont:[UIFont fontWithName:@"Arial Rounded MT Bold" size:17]];
         cell.selectionStyle= UITableViewCellSelectionStyleNone;
        return cell;
        
    }
    else{
        static NSString *cellCity = @"cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellCity];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor colorWithRed:0.286 green:0.827 blue:0.851 alpha:1.000];
        cell.textLabel.text = [_cityArray objectAtIndex:indexPath.row - 1];
        cell.textLabel.textColor = [UIColor colorWithRed:0.961 green:1.000 blue:1.000 alpha:1.000];
        
        [cell.textLabel setFont:[UIFont fontWithName:@"Arial Rounded MT Bold" size:17]];
        
        return cell;
    }
    
}
- (void)backButton:(UIButton *)btu{
      [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row != 0) {
     //   NSLog(@"swdd");
    _cityName = [_cityArray objectAtIndex:indexPath.row - 1];
        NSLog(@"%@",_cityName);
#pragma mark 协议第二步
        
        [self.myDelegate presentDelegateName:_cityName];
        
        [self dismissViewControllerAnimated:YES completion:^{
        
            
        }];
}
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
